3 * Cleanup tables that have valid usernames with no user ID
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that cleans up tables that have valid usernames with no
32 * @ingroup Maintenance
35 class CleanupUsersWithNoId
extends LoggedUpdateMaintenance
{
36 private $prefix, $table, $assign;
37 private $triedCreations = [];
39 public function __construct() {
40 parent
::__construct();
41 $this->addDescription( 'Cleans up tables that have valid usernames with no user ID' );
42 $this->addOption( 'prefix', 'Interwiki prefix to apply to the usernames', true, true, 'p' );
43 $this->addOption( 'table', 'Only clean up this table', false, true );
44 $this->addOption( 'assign', 'Assign edits to existing local users if they exist', false, false );
45 $this->setBatchSize( 100 );
48 protected function getUpdateKey() {
52 protected function doDBUpdates() {
53 $this->prefix
= $this->getOption( 'prefix' );
54 $this->table
= $this->getOption( 'table', null );
55 $this->assign
= $this->getOption( 'assign' );
58 'revision', 'rev_id', 'rev_user', 'rev_user_text',
59 [ 'rev_user' => 0 ], [ 'rev_timestamp', 'rev_id' ]
62 'archive', 'ar_id', 'ar_user', 'ar_user_text',
66 'logging', 'log_id', 'log_user', 'log_user_text',
67 [ 'log_user' => 0 ], [ 'log_timestamp', 'log_id' ]
70 'image', 'img_name', 'img_user', 'img_user_text',
71 [ 'img_user' => 0 ], [ 'img_timestamp', 'img_name' ]
74 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text',
75 [], [ 'oi_name', 'oi_timestamp' ]
78 'filearchive', 'fa_id', 'fa_user', 'fa_user_text',
82 'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text',
86 'recentchanges', 'rc_id', 'rc_user', 'rc_user_text',
94 * Calculate a "next" condition and progress display string
95 * @param IDatabase $dbw
96 * @param string[] $indexFields Fields in the index being ordered by
97 * @param object $row Database row
98 * @return array [ string $next, string $display ]
100 private function makeNextCond( $dbw, $indexFields, $row ) {
103 for ( $i = count( $indexFields ) - 1; $i >= 0; $i-- ) {
104 $field = $indexFields[$i];
105 $display[] = $field . '=' . $row->$field;
106 $value = $dbw->addQuotes( $row->$field );
107 if ( $next === '' ) {
108 $next = "$field > $value";
110 $next = "$field > $value OR $field = $value AND ($next)";
113 $display = join( ' ', array_reverse( $display ) );
114 return [ $next, $display ];
120 * @param string $table Table to migrate
121 * @param string|string[] $primaryKey Primary key of the table.
122 * @param string $idField User ID field name
123 * @param string $nameField User name field name
124 * @param array $conds Query conditions
125 * @param string[] $orderby Fields to order by
127 protected function cleanup(
128 $table, $primaryKey, $idField, $nameField, array $conds, array $orderby
130 if ( $this->table
!== null && $this->table
!== $table ) {
134 $primaryKey = (array)$primaryKey;
135 $pkFilter = array_flip( $primaryKey );
137 "Beginning cleanup of $table\n"
140 $dbw = $this->getDB( DB_MASTER
);
145 // Fetch the rows needing update
148 array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
149 array_merge( $conds, [ $next ] ),
152 'ORDER BY' => $orderby,
153 'LIMIT' => $this->mBatchSize
,
156 if ( !$res->numRows() ) {
160 // Update the existing rows
161 foreach ( $res as $row ) {
162 $name = $row->$nameField;
163 if ( $row->$idField ||
!User
::isUsableName( $name ) ) {
168 if ( $this->assign
) {
169 $id = (int)User
::idFromName( $name );
171 // See if any extension wants to create it.
172 if ( !isset( $this->triedCreations
[$name] ) ) {
173 $this->triedCreations
[$name] = true;
174 if ( !Hooks
::run( 'ImportHandleUnknownUser', [ $name ] ) ) {
175 $id = (int)User
::idFromName( $name, User
::READ_LATEST
);
181 $set = [ $idField => $id ];
182 $counter = &$countAssigned;
184 $set = [ $nameField => substr( $this->prefix
. '>' . $name, 0, 255 ) ];
185 $counter = &$countPrefixed;
191 array_intersect_key( (array)$row, $pkFilter ) +
[
197 $counter +
= $dbw->affectedRows();
200 list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
201 $this->output( "... $display\n" );
206 "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
211 $maintClass = "CleanupUsersWithNoId";
212 require_once RUN_MAINTENANCE_IF_MAIN
;